package com.cqu.hospitalsystem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqu.hospitalsystem.entity.PatiPresRelation;
import com.cqu.hospitalsystem.service.PatiPresService;
import com.cqu.hospitalsystem.utils.result.DataResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("patiPres")
@Tag(name = "PatiPresController", description = "患者处方管理")
public class PatiPresController {
    @Resource
    private PatiPresService patiPresService;

    @PostMapping("getPatipresAll")
    @Operation(summary = "获取所有患者处方", description = "查询所有患者处方")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到患者处方")
    })
    public DataResult getPatipresAll() {
        List<PatiPresRelation> patiPreslist = this.patiPresService.queryAll();
        String result = JSON.toJSONString(patiPreslist);
        String jsStr = "{\"data\":" + result + "}";
        JSONObject js = JSONObject.parseObject(jsStr);
        DataResult dataResult = DataResult.successByData(js);
        dataResult.setcode(777);
        return dataResult;
    }

    @PostMapping("getMediDetails")
    @Operation(summary = "获取药品详情", description = "根据处方ID查询药品详情")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到药品详情")
    })
    public DataResult getMediDetails(@RequestBody @Parameter(description = "患者处方对象") PatiPresRelation patiPresRelation) {
        System.out.println(patiPresRelation.getPrescriptionId());
        List<PatiPresRelation> patiPreslist = this.patiPresService.queryByPId(patiPresRelation.getPrescriptionId());
        String result = JSON.toJSONString(patiPreslist);
        String jsStr = "{\"data\":" + result + "}";
        JSONObject js = JSONObject.parseObject(jsStr);
        DataResult dataResult = DataResult.successByData(js);
        dataResult.setcode(777);
        return dataResult;
    }

    @PostMapping("searchPatipres")
    @Operation(summary = "搜索患者处方", description = "根据患者标识搜索患者处方")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到患者处方")
    })
    public DataResult searchPatipres(@RequestBody @Parameter(description = "患者处方对象") PatiPresRelation patiPresRelation) {
        List<PatiPresRelation> patiPresRelation1 = this.patiPresService.queryById(patiPresRelation.getPatientIdentify());
        String result = JSON.toJSONString(patiPresRelation1);
        String jsStr = "{\"data\":" + result + "}";
        JSONObject js = JSONObject.parseObject(jsStr);
        DataResult dataResult = DataResult.successByData(js);
        dataResult.setcode(777);
        return dataResult;
    }

    @PostMapping("updatepreState")
    @Operation(summary = "更新处方状态", description = "根据处方ID更新处方状态")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "400", description = "更新失败")
    })
    public ResponseEntity<PatiPresRelation> updatepreState(@RequestBody @Parameter(description = "患者处方对象") PatiPresRelation patiPresRelation) {
        return ResponseEntity.ok(this.patiPresService.update(patiPresRelation));
    }

    @PostMapping("returnMedicine")
    @Operation(summary = "退回药品", description = "根据处方ID退回药品")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "退回成功"),
            @ApiResponse(responseCode = "400", description = "退回失败")
    })
    public ResponseEntity<PatiPresRelation> returnMedicine(@RequestBody @Parameter(description = "患者处方对象") PatiPresRelation patiPresRelation) {
        System.out.println(patiPresRelation.getConnectId());
        System.out.println(patiPresRelation.getPrescriptionId());
        return ResponseEntity.ok(this.patiPresService.update_mediState(patiPresRelation));
    }

    @PostMapping("showreturnSys")
    @Operation(summary = "显示退回系统", description = "查询所有退回的处方")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到退回处方")
    })
    public DataResult showreturnSys() {
        List<PatiPresRelation> patiPreslist = this.patiPresService.queryreturnAll();
        String result = JSON.toJSONString(patiPreslist);
        String jsStr = "{\"data\":" + result + "}";
        JSONObject js = JSONObject.parseObject(jsStr);
        DataResult dataResult = DataResult.successByData(js);
        dataResult.setcode(777);
        return dataResult;
    }
}